//
//  GCMMainViewController.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/3/11.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMMainViewController.h"
#import "GCMMyNoticeViewController.h"
#import "GCMAreaViewController.h"
#import "GCMReciveViewController.h"
#import "GCMTaskListViewController.h"
#import "GCMConmonViewController.h"
#import "GCMURLViewController.h"
#import "GCMLoginViewController.h"
#import "GCMReciveDetailViewController.h"
#import "GCMTaskDetailViewController.h"
#import "GCMPushViewController.h"
#import "GCMEquPlanViewController.h"
#import "GCMCBYIJIViewController.h"


#define WIDTH ([UIScreen mainScreen].bounds.size.width - 30)/3

@interface GCMMainViewController ()<UIScrollViewDelegate>

@end

@implementation GCMMainViewController{

    NSMutableArray *_dataSource;
    UIScrollView *_scrollV;
    UIView *_uInfoView;
    UIButton *_noticeBtn;
    UIButton *_reciveBtn;
    UIButton *_homeSearchBtn;
    UIButton *_taskBtn;
    UIButton *_deviceBtn;
    UIButton *_snatchOrderBtn;
    UIButton *_logoutBtn;
    UIButton *_imageGetBtn;

}
-(void)viewWillAppear:(BOOL)animated{

    [self setNavigationBar];
    self.navigationController.navigationBarHidden = YES;
//    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createScrollView];
    [self initData];
    _scrollV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            // 进入刷新状态后会自动调用这个block
        [self downLoadData];
    
    }];
    [self downLoadData];
    

}
//推送消息跳转页
- (void)pushToNotifyControllerWithUserInfo:(NSDictionary *)userInfo{

    NSString *str = [NSString stringWithFormat:@"%@",[userInfo valueForKey:@"typeID"]];
    if ([str isEqualToString:@"2"]) {
        GCMReciveDetailViewController * reciveVC = [[GCMReciveDetailViewController alloc]init];
        reciveVC.idd = [userInfo valueForKey:@"id"];
        reciveVC.type = @"1";
        [self.navigationController pushViewController:reciveVC animated:YES ];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"接待处理";
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationItem.backBarButtonItem = backItem;
   
    }else if ([str isEqualToString:@"1"]){
        GCMTaskDetailViewController * taskVC = [[GCMTaskDetailViewController alloc]init];
        taskVC.idd = [userInfo valueForKey:@"id"];
        taskVC.type = @"1";
        [self.navigationController pushViewController:taskVC animated:YES ];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"任务详情";
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationItem.backBarButtonItem = backItem;
        
    }else if ([str isEqualToString:@"3"]){
    
        GCMPushViewController * pushVC = [[GCMPushViewController alloc]init];
        pushVC.dic = [userInfo valueForKey:@"aps"];
        [self.navigationController pushViewController:pushVC animated:YES];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"通知消息";
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationItem.backBarButtonItem = backItem;
        
    }
}

-(void)createScrollView{

    self.automaticallyAdjustsScrollViewInsets = NO;
    //    创建可以滑动的视图
    _scrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, WIDTH + 20 +19, SCREEN_WIDTH, SCREEN_HEIGHT  - WIDTH - 39)];
    _scrollV.delegate = self;
    //_scrollV.pagingEnabled = YES;
    _scrollV.alwaysBounceHorizontal = NO;
    _scrollV.alwaysBounceVertical = NO;
    _scrollV.showsHorizontalScrollIndicator = NO;
    _scrollV.showsVerticalScrollIndicator = NO;
    _scrollV.bounces = YES;
    _scrollV.contentOffset = CGPointMake(0, 0);
    _scrollV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_scrollV];
//    _scrollV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        // 进入刷新状态后会自动调用这个block
//        [self downLoadData];
//        
//    }];

    
}
-(void)createUI{


    //    用户信息展示按钮
    _uInfoView = [[UIView alloc]initWithFrame:CGRectMake( 10, 29, SCREEN_WIDTH - 20, (SCREEN_WIDTH - 20)/3)];
    _uInfoView.tag = 11;
    _uInfoView.backgroundColor = [UIColor colorWithRed:0.21 green:0.60 blue:0.86 alpha:1];
    [self.view addSubview:_uInfoView];
      //图片
    UIImageView *headImageV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 20, WIDTH - 40, WIDTH - 40)];
    //headImageV.backgroundColor = [UIColor redColor];
    [headImageV setImage:[UIImage imageNamed:@"main_def_usericon"]];
    //[headImageV sd_setImageWithURL:[NSURL URLWithString:[GCMUserInfo instance].pic]];
    headImageV.userInteractionEnabled = YES;
    [headImageV sd_setImageWithURL:[NSURL URLWithString:[GCMUserInfo instance].pic] placeholderImage:[UIImage imageNamed:@"main_def_usericon"]];
    headImageV.userInteractionEnabled = YES;
    headImageV.layer.cornerRadius = (WIDTH - 40)/2;
    headImageV.layer.masksToBounds = YES;
    _imageGetBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, WIDTH - 40,WIDTH - 40)];
    _imageGetBtn.layer.cornerRadius = (WIDTH - 40)/2;
    _imageGetBtn.layer.masksToBounds = YES;
    [_imageGetBtn addTarget:self  action:@selector(onImageGetButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [headImageV addSubview:_imageGetBtn];
    //    增加长按手势
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
    [_uInfoView addGestureRecognizer:longPressGesture];
    [_uInfoView addSubview:headImageV];

    
    UILabel * nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH - 20, 10, WIDTH *2, WIDTH/4)];
    //nameLabel.backgroundColor = [UIColor redColor];
    nameLabel.text = [GCMUserInfo instance].copyright;
    nameLabel.textColor  = [UIColor whiteColor];
    nameLabel.font = [UIFont systemFontOfSize:16];
    [_uInfoView addSubview:nameLabel];
    _logoutBtn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH*2 +20, 15, WIDTH-30, WIDTH/4)];
    //_logoutBtn.backgroundColor = [UIColor redColor];
    [_uInfoView addSubview:_logoutBtn];
    UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/4, 0, _logoutBtn.bounds.size.width - WIDTH/4, WIDTH/4)];
    //label1.backgroundColor = [UIColor redColor];
    label1.text = @"注销";
    label1.textAlignment = NSTextAlignmentRight;
    label1.font = [UIFont systemFontOfSize:15];
    label1.textColor  = [UIColor whiteColor];
    [_logoutBtn addSubview:label1];
    UIImageView *logoutImgV = [[UIImageView alloc]initWithFrame:CGRectMake(_logoutBtn.bounds.size.width - WIDTH/2, 3, WIDTH/4 - 5, WIDTH/4 - 5)];
    [logoutImgV setImage:[UIImage imageNamed:@"main_ic_logout"]];
    [_logoutBtn addTarget:self action:@selector(onlogoutbtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_logoutBtn addSubview:logoutImgV];
    
    UIImageView *uImgV = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - 20, WIDTH/2 - 10 , WIDTH/4, WIDTH/4)];
    [uImgV setImage:[UIImage imageNamed:@"main_ic_user"]];
    [_uInfoView addSubview:uImgV];
    UILabel * uNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH - 20 +WIDTH/4, WIDTH/2 - 10, WIDTH , WIDTH/4)];
    uNameLabel.font = [UIFont systemFontOfSize:14];
    //uNameLabel.backgroundColor = [UIColor redColor];
    uNameLabel.text = [GCMUserInfo instance].realName;
    uNameLabel.textColor  = [UIColor whiteColor];
    [_uInfoView addSubview:uNameLabel];
    
    UILabel * positionLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH - 10  , WIDTH/2 + 20, WIDTH , WIDTH/4)];
    positionLabel.font = [UIFont systemFontOfSize:15];
    positionLabel.text = [NSString stringWithFormat:@"(%@)", [GCMUserInfo instance].position];
    positionLabel.textColor  = [UIColor whiteColor];
    [_uInfoView addSubview:positionLabel];
    
//    展示具体的内容
    int R = 0;
    NSMutableArray *a = [NSMutableArray arrayWithArray:_dataSource];
    //NSLog(@"1111111%@",a);
    if (a.count%2 != 0) {
        for (int i = 0; i < a.count/2; i ++) {
            for (int j = 0; j < 2; j ++) {
                
                UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = CGRectMake(10 + ((10+WIDTH)*j),  (WIDTH+10)*i, WIDTH +(WIDTH*j),WIDTH);
                button.tag  = R;
                [button addTarget:self action:@selector(onButtonClick:) forControlEvents:UIControlEventTouchUpInside];
                UILabel *titileLabel = [[ UILabel alloc]initWithFrame:CGRectMake(5, 5, 60, 20)];
                titileLabel.text = [NSString stringWithFormat:@"%@",[a[R] valueForKey:@"title"]];
                titileLabel.textColor  = [UIColor whiteColor];
                titileLabel.font = [ UIFont systemFontOfSize:15];
                [titileLabel sizeToFit];
                [button addSubview:titileLabel];
                UILabel *readLabel = [[ UILabel alloc]initWithFrame:CGRectMake(0, button.bounds.size.height - 25, button.bounds.size.width-5, 20)];
                readLabel.text = [NSString stringWithFormat:@"%@",[a[R] valueForKey:@"content"]];
                //NSLog(@"%@",[_dataSource valueForKey:@"noticeNum"]);
                readLabel.textColor  = [UIColor whiteColor];
                readLabel.textAlignment = NSTextAlignmentRight ;
                readLabel.font = [ UIFont systemFontOfSize:15];
                [button addSubview:readLabel];
                UIImageView *notoceImageV = [[UIImageView alloc]initWithFrame:CGRectMake(button.bounds.size.width/2- (WIDTH - 60)/ 2, 30, WIDTH - 60, WIDTH - 60)];
                //notoceImageV.backgroundColor = [UIColor redColor];
                [notoceImageV sd_setImageWithURL:[a[R] valueForKey:@"drawable"]];
                [button addSubview:notoceImageV];
                int r = [[[a[R] objectForKey:@"bgColor"] valueForKey:@"r"] intValue];
                int b = [[[a[R] objectForKey:@"bgColor"] valueForKey:@"b"] intValue];
                int g = [[[a[R] objectForKey:@"bgColor"] valueForKey:@"g"] intValue];
                button.backgroundColor = [UIColor colorWithRed:r/256.00 green:g/256.00 blue:b/256.00 alpha:1];
                R++;
                [_scrollV addSubview:button];


            }
            
            UIButton * button1 = [UIButton buttonWithType:UIButtonTypeCustom];
            button1.frame = CGRectMake(10, (WIDTH+10)*(a.count/2), SCREEN_WIDTH-20,WIDTH);
            button1.tag  = R;
            [button1 addTarget:self action:@selector(onButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            int r = [[[a[R] objectForKey:@"bgColor"] valueForKey:@"r"] intValue];
            int b = [[[a[R] objectForKey:@"bgColor"] valueForKey:@"b"] intValue];
            int g = [[[a[R] objectForKey:@"bgColor"] valueForKey:@"g"] intValue];
            button1.backgroundColor = [UIColor colorWithRed:r/256.00 green:g/256.00 blue:b/256.00 alpha:1];
            UILabel *titileLabel = [[ UILabel alloc]initWithFrame:CGRectMake(5, 5, 60, 20)];
            titileLabel.text = [NSString stringWithFormat:@"%@",[a[R] valueForKey:@"title"]];
            titileLabel.textColor  = [UIColor whiteColor];
            titileLabel.font = [ UIFont systemFontOfSize:15];
            [titileLabel sizeToFit];
            [button1 addSubview:titileLabel];
            UILabel *readLabel = [[ UILabel alloc]initWithFrame:CGRectMake(0, button1.bounds.size.height - 25, button1.bounds.size.width-5, 20)];
            readLabel.text = [NSString stringWithFormat:@"%@",[a[R] valueForKey:@"content"]];
            //NSLog(@"%@",[_dataSource valueForKey:@"noticeNum"]);
            readLabel.textColor  = [UIColor whiteColor];
            readLabel.textAlignment = NSTextAlignmentRight ;
            readLabel.font = [ UIFont systemFontOfSize:15];
            [button1 addSubview:readLabel];
            UIImageView *notoceImageV = [[UIImageView alloc]initWithFrame:CGRectMake(button1.bounds.size.width/2- (WIDTH - 60)/ 2, 30, WIDTH - 60, WIDTH - 60)];
            //notoceImageV.backgroundColor = [UIColor redColor];
            [notoceImageV sd_setImageWithURL:[a[R] valueForKey:@"drawable"]];
            [button1 addSubview:notoceImageV];
            [_scrollV addSubview:button1];
        }
    }else{
        for (int i = 0; i < a.count/2; i ++) {
            for (int j = 0; j < 2; j ++) {
                
                UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = CGRectMake(10 + ((10+WIDTH)*j),  (WIDTH+10)*i, WIDTH +(WIDTH*j),WIDTH);
                button.tag  = R;
                [button addTarget:self action:@selector(onButtonClick:) forControlEvents:UIControlEventTouchUpInside];
                
                UILabel *titileLabel = [[ UILabel alloc]initWithFrame:CGRectMake(5, 5, 60, 20)];
                titileLabel.text = [NSString stringWithFormat:@"%@",[a[R] objectForKey:@"title"]];
                titileLabel.textColor  = [UIColor whiteColor];
                titileLabel.font = [ UIFont systemFontOfSize:15];
                [titileLabel sizeToFit];
                [button addSubview:titileLabel];
                UILabel *readLabel = [[ UILabel alloc]initWithFrame:CGRectMake(0, button.bounds.size.height - 25, button.bounds.size.width-5, 20)];
                readLabel.text = [NSString stringWithFormat:@"%@",[a[R] valueForKey:@"content"]];
                //NSLog(@"%@",[_dataSource valueForKey:@"noticeNum"]);
                readLabel.textColor  = [UIColor whiteColor];
                readLabel.textAlignment = NSTextAlignmentRight ;
                readLabel.font = [ UIFont systemFontOfSize:15];
                [button addSubview:readLabel];
                UIImageView *notoceImageV = [[UIImageView alloc]initWithFrame:CGRectMake(button.bounds.size.width/2- (WIDTH - 60)/ 2, 30, WIDTH - 60, WIDTH - 60)];
                //notoceImageV.backgroundColor = [UIColor redColor];
                [notoceImageV sd_setImageWithURL:[a[R] valueForKey:@"drawable"]];
                [button addSubview:notoceImageV];

                int r = [[[a[R] objectForKey:@"bgColor"] valueForKey:@"r"] intValue];
                int b = [[[a[R] objectForKey:@"bgColor"] valueForKey:@"b"] intValue];
                int g = [[[a[R] objectForKey:@"bgColor"] valueForKey:@"g"] intValue];
                button.backgroundColor = [UIColor colorWithRed:r/256.00 green:g/256.00 blue:b/256.00 alpha:1];
                R++;
                [_scrollV addSubview:button];
            }
        }
    }
}


//处理长按事件
- (void)handleLongPressGesture:(UILongPressGestureRecognizer *)longPressGesture{
    if (longPressGesture.state == UIGestureRecognizerStateBegan) {
        
        GCMURLViewController *urlVC = [[GCMURLViewController alloc]init];
        [self.navigationController pushViewController:urlVC animated:YES];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"系统设置";
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationItem.backBarButtonItem = backItem;
        return;
        
    }
    
}
    
-(void)onButtonClick:(UIButton *)sender{
//    统计点击次数
    [GCMMainHttpRequst ClickTimes:[_dataSource[sender.tag] valueForKey:@"title"] Success:^(id times, NSError *err) {
//        NSLog(@"测试统计%@次数成功",[_dataSource[sender.tag] valueForKey:@"title"]);
    }];
    
    if ([[_dataSource[sender.tag] valueForKey:@"title"] isEqualToString:@"抄表录数"]) {
        GCMCBYIJIViewController *cbVC =[[GCMCBYIJIViewController alloc]init];
        [self.navigationController pushViewController:cbVC animated:YES];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"抄表录数";
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationItem.backBarButtonItem = backItem;
    }
    if ([[_dataSource[sender.tag] valueForKey:@"title"] isEqualToString:@"我的接待"]) {
        GCMReciveViewController *main =[[GCMReciveViewController alloc]init];
        [self.navigationController pushViewController:main animated:YES];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"我的接待";
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationItem.backBarButtonItem = backItem;
    }
    if ([[_dataSource[sender.tag] valueForKey:@"title"] isEqualToString:@"住户查询"]) {
        GCMAreaViewController *main =[[GCMAreaViewController alloc]init];
        [self.navigationController pushViewController:main animated:YES];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"小区选择";
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationItem.backBarButtonItem = backItem;
    }
    if ([[_dataSource[sender.tag] valueForKey:@"title"] isEqualToString:@"我的任务"]) {
        GCMTaskListViewController *task =[[GCMTaskListViewController alloc]init];
        [self.navigationController pushViewController:task animated:YES];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"我的任务";
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationItem.backBarButtonItem = backItem;
    }
    if ([[_dataSource[sender.tag] valueForKey:@"title"] isEqualToString:@"设备计划"]) {
        GCMEquPlanViewController *plan =[[GCMEquPlanViewController alloc]init];
        [self.navigationController pushViewController:plan animated:YES];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"设备计划";
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationItem.backBarButtonItem = backItem;
    }
    if ([[_dataSource[sender.tag] valueForKey:@"title"] isEqualToString:@"我的待办"]) {
        
        GCMConmonViewController * conmonView =[[GCMConmonViewController alloc]init];
        conmonView.type = @"1";
        [self.navigationController pushViewController:conmonView animated:YES];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"OA办公";
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationItem.backBarButtonItem = backItem;
    }
    if ([[_dataSource[sender.tag] valueForKey:@"title"] isEqualToString:@"工作计划"]) {
        
        GCMConmonViewController * conmonView =[[GCMConmonViewController alloc]init];
        conmonView.type = @"3";
        [self.navigationController pushViewController:conmonView animated:YES];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"OA办公";
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationItem.backBarButtonItem = backItem;
    }
    if ([[_dataSource[sender.tag] valueForKey:@"title"] isEqualToString:@"工作日志"]) {
        
        GCMConmonViewController * conmonView =[[GCMConmonViewController alloc]init];
        conmonView.type = @"4";
        [self.navigationController pushViewController:conmonView animated:YES];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"OA办公";
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationItem.backBarButtonItem = backItem;
    }
    if ([[_dataSource[sender.tag] valueForKey:@"title"] isEqualToString:@"计划监督"]) {
        
        GCMConmonViewController * conmonView =[[GCMConmonViewController alloc]init];
        conmonView.type = @"5";
        [self.navigationController pushViewController:conmonView animated:YES];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"OA办公";
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationItem.backBarButtonItem = backItem;
    }
    if ([[_dataSource[sender.tag] valueForKey:@"title"] isEqualToString:@"日程安排"]) {
        
        GCMConmonViewController * conmonView =[[GCMConmonViewController alloc]init];
        conmonView.type = @"2";
        [self.navigationController pushViewController:conmonView animated:YES];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"OA办公";
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationItem.backBarButtonItem = backItem;
    }
    if ([[_dataSource[sender.tag] valueForKey:@"title"] isEqualToString:@"我的通知"]) {
        
        GCMConmonViewController * conmonView =[[GCMConmonViewController alloc]init];
        conmonView.type = @"6";
        [self.navigationController pushViewController:conmonView animated:YES];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"OA办公";
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationItem.backBarButtonItem = backItem;
    }
}

-(void)downLoadData{

//    NSLog(@"ppppppppppppppppp%@",[GCMUserInfo instance].departId);
    [GCMMainHttpRequst postMainPageInfoWithUserId:[GCMUserInfo instance].uid WithDepartID:[GCMUserInfo instance].departId WithuserName:[GCMUserInfo instance].realName Success:^(id MainPage, NSError *err) {
        [_dataSource removeAllObjects];
        //_dataSource  = [MainPage valueForKey:@"data"] ;
        [_dataSource addObjectsFromArray:[MainPage valueForKey:@"data"]];
//        NSLog(@"主页======%@",MainPage);
        [self setScrollViewHeight];
        [self createUI];
        [_scrollV.mj_header endRefreshing];
        
    }];
}
//设置主页面scroll的高度
-(void)setScrollViewHeight{

    int n = _dataSource.count%2;
    if (n == 0) {
        _scrollV.contentSize = CGSizeMake(SCREEN_WIDTH,(_dataSource.count/2)*(WIDTH+10));
    }else{
        _scrollV.contentSize = CGSizeMake(SCREEN_WIDTH,(_dataSource.count/2 + 1)*(WIDTH+10));
    }
    
}
-(void)initData{

       _dataSource = [[NSMutableArray alloc]init];
}
-(void)setNavigationBar{
    
    self.navigationController.navigationBarHidden = NO;
    
    self.navigationController.navigationBar.barTintColor =[UIColor colorWithRed:0.12 green:0.34 blue:0.57 alpha:1.0];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:@"新增" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(GotoSettings)
          forControlEvents:UIControlEventTouchUpInside];
    rightButton.frame = CGRectMake(0, 2,40, 40);
    UIBarButtonItem *menuRButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = menuRButton;
    
}
-(void)GotoSettings{

    
}


-(void)onbuttonclick{
    


}



// 照片获取
-(void)onImageGetButtonClick{
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"请选择" message:nil preferredStyle:0];
    [alert addAction:[UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            [self LoadImageWith:UIImagePickerControllerSourceTypeCamera];
        }
        else
        {
            UIAlertView * a = [[UIAlertView alloc]initWithTitle:@"本机不支持" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [a show];
        }
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self LoadImageWith:UIImagePickerControllerSourceTypePhotoLibrary];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
    
}
-(void)LoadImageWith:(UIImagePickerControllerSourceType)type
{
    UIImagePickerController * pick = [[UIImagePickerController alloc]init];
    pick.sourceType=type;
    pick.delegate = self;
    pick.allowsEditing=self;
    [self presentViewController:pick animated:NO completion:nil];
    
}
//代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc]init];
    parameters[@"userName"] = [GCMUserInfo instance].realName;
    parameters[@"userId"] =  [GCMUserInfo instance].uid;
    NSData *data = UIImageJPEGRepresentation(image, 0.4);
    NSString* encodeResult = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
//    NSLog(@"%@",encodeResult);
    parameters[@"images"] = encodeResult;
//    NSLog(@"测试数据%@",parameters);
    [GCMTaskHttpRequst postDataWithParmerts:parameters WithUrl:IMAGECHANGE_URL Success:^(id obj, NSError *err) {
        //NSLog(@"测试成功了%@",obj);
        // NSLog(@"测试成功了");
    }];

    [_imageGetBtn setImage:image forState:UIControlStateNormal];
    [self dismissViewControllerAnimated:NO completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    NSLog(@"已取消选择");
    [self dismissViewControllerAnimated:NO completion:nil];
}



//退出登录
-(void)onlogoutbtnClick{
    
    UIAlertView * alertview5 = [[UIAlertView alloc] initWithTitle:@"确定要退出登陆？" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alertview5.delegate = self;
    [alertview5 show];
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        
        //清空所有的缓存到本地的信息
        
        //        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"password"];
        //        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"username"];
   //     取出缓存
            
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"arr"];

    
//        NSDictionary *defaultsDictionary = [[NSUserDefaults standardUserDefaults]dictionaryRepresentation];
//        for (NSString *key in [defaultsDictionary allKeys]) {
//            [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
//             NSLog(@" NSHomeDirectory - %@",key);
//        }
//        [[NSUserDefaults standardUserDefaults] synchronize];
        
        
//        NSUserDefaults *s = [NSUserDefaults standardUserDefaults];
//        [s setObject:@"2" forKey:@"tag"];
//        [s synchronize];

      
        
        GCMLoginViewController *login = [[GCMLoginViewController alloc]init];
//        [self.navigationController popViewControllerAnimated:YES];
        UINavigationController *navc = [[UINavigationController alloc]initWithRootViewController:login];
        [self presentViewController:navc animated:YES completion:nil];
        //        NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
        //        [userDefaults setObject:@"" forKey:@"useridSave"];
        //        [userDefaults synchronize];
        //[XRQJpush setBieming:@""];
        // NSLog(@"用户的userid----%@",sid);
        
    }
}


-(void)viewWillDisappear:(BOOL)animated{

    self.navigationController.navigationBar.hidden = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
